<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ page import="com.easyui.common.SelNote, com.easyui.bean.HeadNote" %>
<%@ page import="java.util.Map, java.util.List" %>
<%@ page import="com.easyui.common.UserLogin" %>
<%
	String userID = (String)session.getAttribute("userID");
	String role = (String)session.getAttribute("role");
	String userName = "";
	if(userID == null || role == null || userID.trim().equals("") || role.trim().equals("")) {
		response.sendRedirect("../login.html");
	} else {
		if(role.equals("admin")) {
			userName = "admin";	
		} else {
			userName = new UserLogin().getName(role, userID);
		}
	
	
		int nno = Integer.parseInt(request.getParameter("nno"));
		int curpage = Integer.parseInt(request.getParameter("page"));
		
		//主题帖信息
		List<HeadNote> headList = new SelNote().getNote(nno);//主题帖
		HeadNote hnm = headList.get(0);
		
		//回复贴信息
		List<Object> mainNote = new SelNote().getNote(nno, curpage);//回复贴
		Map<String, Integer> noteNum = (Map<String, Integer>)mainNote.get(0);
		List<HeadNote> hnList = (List<HeadNote>)mainNote.get(1);
		
		int replysize = 0;
		if(hnList == null) {
			replysize = 0;
		} else {
			replysize = hnList.size();
		}
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><%= hnm.getNtitle() %></title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<script type="text/javascript" src="../js/jquery-easyui-1.3.5/jquery.min.js" charset="UTF-8"></script>
	<script type="text/javascript" src="../js/jquery-easyui-1.3.5/jquery.easyui.min.js" charset="UTF-8"></script>
	<link rel="stylesheet" href="../js/jquery-easyui-1.3.5/themes/default/easyui.css" type="text/css"></link>
	<link rel="stylesheet" href="../js/jquery-easyui-1.3.5/themes/icon.css" type="text/css"></link>
	<link rel="stylesheet" href="../css/style.css" type="text/css"></link>
	<script type="text/javascript" src="../js/mine.js"></script>
	<script type="text/javascript" src="../js/jquery-easyui-1.3.5/locale/easyui-lang-zh_CN.js" charset="UTF-8"></script>
	
	<script type="text/javascript">
		var mainReplyForm;
		$(function(){
			mainReplyForm = $('#mainReplyForm').form({
				url:'../servlet/MainReplyServlet',
				onSubmit: function(){   
					var result = $.trim($("#ncont").val()) != "" ? true:false; 
					if(!result) {
						$.messager.alert('回复内容不能为空', '回复内容不能为空！', 'error');
						return false;
					}
					
   				 },
				success:function(msg){
					if(msg == 'ok') {
						alert('回复成功');
						window.location.href= "discussDetail.jsp?nno=<%= nno %>&page=<%= noteNum.get("total") %>";
					} else {
						alert('回复失败');
					}
				}
			});
		});
		
		submitReplySon = function(i) {
			var isValid = $.trim($("#ncont_"+ i).val()) != "" ? true:false; 
			if(!isValid){
				$.messager.alert('回复内容不能为空', '回复内容不能为空！', 'error');
				return false;
			}
			$.ajax({
				type:'post',
				url:'../servlet/SonReplyServlet',  //无附件
				data:$('#replyForm_' + i).serialize(),
				dataType:'text',
				success:function(msg){
					console.log(msg);
					if(msg == 'ok') {
						alert('回复成功');
						window.location.href= "discussDetail.jsp?nno=<%= nno %>&page=<%= curpage %>"; 
					} else {
						alert('回复失败');
					}
				}
			  });
			
		}
		
		delNote = function(nno, pid) {
			console.info("nno:" + nno);
			console.info("pid:" + pid);
			if(confirm('确定要删除吗？')) {
				$.ajax({
					type:'post',
					url:'../servlet/DelNoteServlet',  
					data:{
						nno:nno,
						pid:pid
					},
					dataType:'json',
					success:function(r){
						if(r && r.success) {
							alert(r.msg);
							window.location.href= "discussDetail.jsp?nno=<%= nno %>&page=<%= curpage %>"; 
						} else {
							alert(r.msg);
						}
					}
				});
			}
		}
		
		cleanReply = function() {
			$('#ncont').val('');
			$('#attach').val('');
		}
		
		showReply = function(i) {
			$('#replydiv_' + i).css('display','block');
		}
		
		disReply = function(i) {
			$('#replydiv_' + i).css('display','none');
		}
		
		quit = function() {
  			$.messager.confirm('提示','确定要退出吗?',function(b) {
				if(b) {
					$.ajax({
						url:'../servlet/QuitLoginServlet',
						dataType:'json',
						success:function(r) {
							if(r && r.success) {
								window.location.href = "../login.html";
							} else {
								$.messager.alert('出错啦','退出登录失败.','error');
							}
						}
					});
				}
  			});
		}
	</script>
</head>

<body style="margin:0;">
	<div style="height:60px;overflow:hidden;background-color:#E0ECFF;margin-bottom:1px;">		
		<div style="float:right;margin-top:30px;">
            	<p>欢迎您登陆系统,&nbsp;
            		<span style="color:#F90;"><%= userName %>&nbsp;&nbsp;&nbsp;</span>
            		<img src="../images/exit.png" align="texttop"  /> <a href="javascript:void(0)" onclick="quit();">退出</a> 
            		<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
            	</p>
            </div>
		<div>
			<p style="width:400px; margin:0 auto;"><img src="../images/font.png" style="margin-top:5px;" /></p>
            </div>     
	</div>
		
	    <div>
		<!--      本页主题的标题        -->
		<div>
			<table cellSpacing="0" cellPadding="0"  style="width:100%;position:relative;">
				<tr>
					<th class="h" style="height:25px;">主题: <%= hnm.getNtitle() %>
					    <div style="position:absolute;right:0;top:0;">
						    <div class="pages" style="display:inline-block;vertical-align:middle;">
							<% if(noteNum.get("current") != 1) { %>
								<a href="discussDetail.jsp?nno=<%= nno %>&page=<%= 1 %>"><<</a>&nbsp;
								<a href="discussDetail.jsp?nno=<%= nno %>&page=<%= noteNum.get("current") - 1 %>">上一页</<a href="	"></a>>&nbsp;&nbsp;
							<% } %>
								<a href="javascript:void(0);"><%= noteNum.get("current") %></a>&nbsp;
							<% if(noteNum.get("current") != noteNum.get("total")) { 
							%> 
								<a href="discussDetail.jsp?nno=<%= nno %>&page=<%= noteNum.get("current") + 1 %>">下一页</<a href="	"></a>>&nbsp;&nbsp; 
								<a href="discussDetail.jsp?nno=<%= nno %>&page=<%= noteNum.get("total") %>">>></a>&nbsp; 
							<% } %>
								Pages:&nbsp;(&nbsp;<%= noteNum.get("current") %>&nbsp;/&nbsp;<%= noteNum.get("total") %>&nbsp;
								total&nbsp;)&nbsp; 
							</div>
							
							<!--      回复、新帖按钮       -->
							<div style="display:inline-block;vertical-align:middle;">
								<a href="#mainr"><img src="../images/reply.gif" border="0" id="td_post"></a>
							</div>
						</div>
					</th>
				</tr>
				<tr class="tr2">
					<td>&nbsp;</td>
				</tr>
			</table>
		</div>

		<!--      主题帖        -->
		<% if(curpage == 1) { 
			List<Map> infoMap = hnm.getPubinfo();
			Map<String, Object> info = infoMap.get(0);
		%>
		<div class="t">
			<table style="BORDER-TOP-WIDTH: 0px; TABLE-LAYOUT: fixed" cellSpacing="0" cellPadding="0" width="100%">
				<tr class="tr1">
					<th style="width: 15%">
						<br/>
						<b>楼主</b><br /> 
						<div style="float:left"><img src="../images/head/1.gif" /><br /></div>
							<b><%= hnm.getPubname() %></b>&nbsp;&nbsp;
							<br />
							<%  if(((String)info.get("sex")).equals("female")) { %> 女&nbsp;&nbsp;
							<% 	} else {  %> 男&nbsp;&nbsp; <% } %>
							<% if(hnm.getPubtype().equals("teacher")) { %> 
								<br />教师&nbsp;&nbsp;
								<br />
								<br />
							<% } else { %> 
								<br />学生&nbsp;&nbsp;
								<br /><%= (String)info.get("stype") %>&nbsp;&nbsp;
								<br /><%= (String)info.get("major") %>&nbsp;&nbsp;
							<% } %>
						<br/>
						<br/>
						<br/>
					</th>
					<th>
						<br>
						<div><%= hnm.getNcont() %></div>
						<br>
						<br>
						<br>
						<% 
							if(hnm.getAttach().equals("1")) {
								List<Map> alist = hnm.getAttachList();
								if(alist != null && alist.size() > 0) {
									for(int j=0; j<alist.size(); j++) {
										Map<String, Object> amap = alist.get(j); 
						%>
						<span class="gray">附件：<a href="../servlet/DownloadServlet?ano=<%= (Integer)amap.get("ano")%>" ><%= (String)amap.get("aname") %></a></span><br/>
						<%
									}
								}
							}
						%>
						<div class="tipad gray">
							<span><%= hnm.getPtime() %></span> &nbsp; 
						</div>
						<br />
					</th>
				</tr>
			</table>
		</div>
		<% } %>
		
		
		<!--      回复        -->
		<% 
			if(hnList != null && hnList.size() > 0) {
				for(int i=0; i<hnList.size(); i++) {
					HeadNote hn = hnList.get(i);
					List<Map> infoMap = hn.getPubinfo();
					Map<String, Object> info = infoMap.get(0);
		%>
		<div class="t">
			<table style="BORDER-TOP-WIDTH: 0px; TABLE-LAYOUT: fixed" cellSpacing="0" cellPadding="0" width="100%">
				<tr class="tr1">
					<th style="width: 15%">
						<br/>
						<b><%= noteNum.get("pagesize")*(curpage -1) +i+1 %>楼</b><br /> 
						<div style="float:left"><img src="../images/head/1.gif" /><br /></div>
							<b><%= hn.getPubname() %></b>&nbsp;&nbsp;
							<br />
							<%  if(((String)info.get("sex")).equals("female")) { %> 女&nbsp;&nbsp;
							<% 	} else {  %> 男&nbsp;&nbsp; <% } %>
							<% if(hn.getPubtype().equals("teacher")) { %> 
								<br />教师&nbsp;&nbsp;
							<% } else { %> 
								<br />学生&nbsp;&nbsp;
								<br /><%= (String)info.get("stype") %>&nbsp;&nbsp;
								<br /><%= (String)info.get("major") %>&nbsp;&nbsp;
							<% } %>
						<br/>
						<br/>
						<br/>
					</th>
					<th>
						<br>
						<div><%= hn.getNcont() %></div>
						<br>
						<br>
						<br>
						<% 
							if(hn.getAttach().equals("1")) {
								List<Map> alist = hn.getAttachList();
								if(alist != null && alist.size() > 0) {
									for(int j=0; j<alist.size(); j++) {
										Map<String, Object> amap = alist.get(j); 
						%>
						<span class="gray">附件：<a href="../servlet/DownloadServlet?ano=<%= (Integer)amap.get("ano")%>" ><%= (String)amap.get("aname") %></a></span><br/>
						<%
									}
								}
							}
						%>
						<div class="tipad gray">
							<span><%= hn.getPtime() %></span> &nbsp; 
							<% 
								if(role.trim().equals(hn.getPubtype().trim()) && userID.trim().equals(hn.getPubno().trim()) && hn.getIsleaf().equals("1")) {
							%>
							<a href="javascript:void(0);" onclick="delNote(<%= hn.getNno() %>, <%= hn.getPid() %>)">[删除]</a>  
							<%  } %>
							<span><a href="javascript:void(0);" onclick="showReply(<%= i %>);">[回复]</a></span>
						</div>
						
						<!--    帖子里的回复        -->
						<% 
							int sonreply = 0; 
							if(hn.getIsleaf().equals("0")) { //有回复
								List<HeadNote> sonList = new SelNote().getSonNote(nno, hn.getNno());
								if(sonList != null && sonList.size() > 0) {
									sonreply = sonList.size();
									for(int m=0; m<sonList.size(); m++) {
										HeadNote son = sonList.get(m);
						%>
						<div class="tipson">
							<table style="BORDER-TOP-WIDTH: 0px; TABLE-LAYOUT: fixed" cellSpacing="0" cellPadding="0" width="100%">
								<tr>
								<th>
									<b><%= son.getPubname() %></b>&nbsp;&nbsp;
									<span><%= son.getNcont() %></span>
									<br />
									<span class="gray"><%= son.getPtime() %></span> &nbsp; 
									<% 
										if(role.trim().equals(son.getPubtype().trim()) && userID.trim().equals(son.getPubno().trim())) {
									%>
									<a href="javascript:void(0);" onclick="delNote(<%= son.getNno() %>, <%= son.getPid() %>)">[删除]</a> 
									<% } %>
								</th>
								</tr>
							</table>
						</div>
						<% 
									}
								}
							} else {
								sonreply = 0;
							}
						%>
						
						<div class="tipson" id="replydiv_<%= i %>" style="display:none;" >
						<form id="replyForm_<%= i %>" method="post">
							<table cellSpacing="0" cellPadding="0" align="center">
								<input type="hidden" name="rootid" id="rootid" value="<%= hn.getRootid() %>" />
								<input type="hidden" name="pid" id="pid" value="<%= hn.getNno() %>" />
								<input type="hidden" name="pno" id="pno" value="<%= hn.getPno() %>" />
								<input type="hidden" name="sonreply" id="sonreply" value="<%= sonreply %>" />
								<tr>
									<th colSpan=2>
										<textarea name="ncont"  id="ncont_<%= i %>" rows="5" cols="42"></textarea>
									</th>
								</tr>
								<tr>
									<th colSpan="2">
										<%-- <input class="btn" type="submit" value="提 交" />
										<input class="btn" type="reset" onclick="disReply(<%= i %>);" value="取消" /> --%>
										<a href="javascript:void(0);" class="btn" onclick="submitReplySon(<%= i %>)" >提 交</a>
										<a href="javascript:void(0);" class="btn" onclick="disReply(<%= i %>);" />取消</a>
									</th>
								</tr>
							</table>
						</form>
						</div>
						
						<br />		
					</th>
				</tr>
			</table>
		</div>
	</div>
	<% 
			}
		} 
	%>
	
	
	
	
	<!--  翻 页         -->
	<div class="pages" style="float:right">
	<% if(noteNum.get("current") != 1) { %>
		<a href="discussDetail.jsp?nno=<%= nno %>&page=<%= 1 %>"><<</a>&nbsp;
		<a href="discussDetail.jsp?nno=<%= nno %>&page=<%= noteNum.get("current") - 1 %>">上一页</a>&nbsp;&nbsp;
	<% } %>
		<a href="javascript:void(0);"><%= noteNum.get("current") %></a>&nbsp;
	<% if(noteNum.get("current") != noteNum.get("total")) { 
	%> 
		<a href="discussDetail.jsp?nno=<%= nno %>&page=<%= noteNum.get("current") + 1 %>">下一页</a>&nbsp;&nbsp; 
		<a href="discussDetail.jsp?nno=<%= nno %>&page=<%= noteNum.get("total") %>">>></a>&nbsp; 
	<% } %>
		Pages:&nbsp;(&nbsp;<%= noteNum.get("current") %>&nbsp;/&nbsp;<%= noteNum.get("total") %>&nbsp;
		total&nbsp;)&nbsp; 
	</div>
	<br/>
	
	<a name="mainr">&nbsp;</a>
	<div class="mainReply" id="replydiv" >
	<form id="mainReplyForm" method="post" enctype="multipart/form-data">
		<table cellSpacing="0" cellPadding="0" align="center" width="100%">
			<input type="hidden" name="rootid" id="rootid" value="<%= hnm.getNno() %>" />
			<input type="hidden" name="pno" id="pno" value="<%= hnm.getPno() %>" />
			<input type="hidden" name="replysize" id="replysize" value="<%= replysize %>" />
			<tr>
				<td class="h" colSpan="2"> <b>回复</b> </td>
			</tr>
			<tr class="tr3">
				<td vAlign=top><b>内容</b></td>
				<td>
					<textarea name="ncont" id="ncont" rows="10" cols="70"></textarea>
				</td>
			</tr>
			<tr class="tr3 addfile">
				<td><b>附件</b></td>
				<td><input type="file" name="attach" id="attach" /></td>
			</tr>
			<tr style="width:70px;display:none;" id="hiddenbox"> <!--往后台传送数据的容器-->	
			</tr>
			<tr>
				<td colSpan="2" align="right">
					<a href="javascript:void(0);" class="btn" onclick="mainReplyForm.submit();" />提 交</a>
					<a href="javascript:void(0);" class="btn" onclick="cleanReply();" />重 置</a> 
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				</td>
			</tr>
		</table>
	</form>
	</div>
	<br/>
	<br/>
	<br/>
<script type="text/javascript" src="../js/addFile.js" charset="UTF-8"></script>  	
</body>
</html>

<%
	}
%>

